<?php
namespace app\index\controller;
use think\Db;

header('Access-Control-Allow-Origin:*');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept"); 

class Food extends Base
{
    //食物列表
    public function index()
    {
      //但提交的当前页为空时，设置流加载默认页面，
      $pagenum=empty($_GET['pagenum'])?1:$_GET['pagenum'];
      //设置默认显示条数
      $page=empty($_GET['page'])?4:$_GET['page'];

      $food=model('food');
      //添加标签查询条件
      $where=$_GET['typeid'] == -1?array('f.is_del'=>0):array('f.is_del'=>0,'f.type'=>$_GET['typeid']);
      //添加模糊查询条件
      if($_GET['search'] != '' ){
        $where['title']=array('like','%'.$_GET['search'].'%');
      } 
      //查询条数
      $count=$food->alias('f')->where($where)->count();
      //异常
      if($count == 0){
        $result=array(
          'status' =>1
        );
        echo json_encode($result);
        exit;
      }
      //最大页数
      $maxpage=ceil($count/$page);
      //最小页数
      $minpage=1;
      //当前页在最大和最小之间
      if($pagenum > $maxpage){
        $pagenum=$maxpage;
      }elseif($pagenum < $minpage){
        $pagenum=$minpage;
      }
      $showpage=($pagenum-1)*$page;
      //查询
      $fooddata=$food->alias('f')->join('td_foodtype t','f.type = t.id')->field('f.*,t.typename')->where($where)->limit($showpage,$page)->select();
      foreach ($fooddata as $k => $v) {
        if($v['assess'] > 0){
            $fooddata[$k]['evaluate']=round($v['assess']/$v['assess_count']);
        }else{
            $fooddata[$k]['evaluate']=0;
        }

      }
      //返回
      if($fooddata){
        $result=array(
          'status'=>0,
          'info' => $fooddata,
          'maxpage' =>$maxpage
        );
      }else{
        $result=array(
          'status' =>1
        );
      }
      echo json_encode($result);
    }
    //食物类别
    public function type(){
        $foodtype=model('foodtype');
        $data=$foodtype->where(array('is_del'=>0 ))->select();
        if($data){
          $res=array(
              'status' => 0,
              'info' =>$data
          );
        }else{
          $res=array(
              'status' => 1
          );
        }
        echo json_encode($res);
    }
    //食物详情
    public function detail(){
    if(empty($_GET['fid'])){
          echo json_encode(array('status'=>1,'info'=>'缺少参数'));
      exit;
    }
    $fid=$_GET['fid'];
    $food=model('food');
    $fooddata=$food->alias('f')->join('td_foodtype t','f.type = t.id')->field('f.*,t.typename')->where(array('f.is_del'=>0,'f.id'=>$fid))->find();
      $mouth_time=get_mouth_time();
      $order=model('order');
      //月销量
      $fooddata['mouth_sales']=$order->where(array('food_id'=>$fooddata['id'],'pay'=>1))->where('create_time','between',[$mouth_time[0],$mouth_time[1]])->count();
      //总销量
      $fooddata['total_sales']=$order->where(array('food_id'=>$fooddata['id'],'pay'=>1))->count();
      //运费
      $fooddata['freight']=4;
      $fooddata['img_area']=explode(',',$fooddata['img_area']);
      $fooddata['label']=explode(',',$fooddata['label']);
      if($fooddata['assess'] >0){
        $fooddata['evaluate'] = round($fooddata['assess'] / $fooddata['assess_count']);
      }else{
        $fooddata['evaluate'] = 0;
      }
      if(!empty($fooddata['id'])){
        $result=array(
          'info'=>$fooddata,
          'status' =>0
        );
      }else{
        $result=array(
          'status'=>1
        );
      }
      echo json_encode($result);
    }
    //加入购物车
    public function joincar(){
        $goodsdata=$_POST['goods'];
        $result = $this->validate($goodsdata,'shopcar');
        if(true !== $result){
            $res =array(
              'status' => 1,
              'info'  => $result
            );
            echo json_encode($res);
            exit;
        }
        $shopcar=model('shopcar');
        $response=$shopcar->havegoods($goodsdata['uid'],$goodsdata['fid']);
        if($response){
            $res =array(
              'status' => 1,
              'info'  => '已加入购物车'
            );
            echo json_encode($res);
            exit;
        }
        $goodsdata['create_time']=time();
        $goodsdata['update_time']=time();
        $data=$shopcar->insert($goodsdata);
        if($data){
            $res=array(
              'status' =>0
            );
        }else{
            $res=array(
              'status' =>1,
              'info' =>'加入购物车失败'
            );
        }
        echo json_encode($res);
    }
    //获取购物车数据
    public function getcardata(){
        if(empty($_GET['uid'])){
          echo json_encode(array('status'=>1,'info'=>'缺少参数'));
            exit;
        }
        $shopcar=model('shopcar');
        $response['data']=$shopcar->alias('s')->join('td_food f','s.fid=f.id')->field('s.*,f.title')->where(array('uid'=>$_GET['uid']))->select();
        $response['count']=$shopcar->alias('s')->where(array('s.uid'=>$_GET['uid']))->count();
        if($response){
          $res=array(
            'status' =>0,
            'info' =>$response['data'],
            'count'=>$response['count']
          );
        }else{
          $res=array(
            'status' =>1
          );
        }
        echo json_encode($res);
    }
    //移除购物车
    public function removecar(){

        if(empty($_GET['id'])){
          echo json_encode(array('status'=>1,'info'=>'缺少参数'));
            exit;
        }
        $shopcar=model('shopcar');
        $response=$shopcar->where(array('id'=>$_GET['id']))->delete();
        if($response){
          $res=array(
            'status' =>0
          );
        }else{
          $res=array(
            'status' =>1
          );
        }
        echo json_encode($res);
    }
}
